#!/usr/bin/python
# Filename: compute_em_rep.py
import math;
import sys;

f = file(sys.argv[1]);
un_count = dict();
bi_count = dict();
no_count = dict();
for line in f:
	words = line.strip().split(' ');
	if words[1] == 'NONTERMINAL':
		no_count[words[2]] = int(words[0]);
	if words[1] == "UNARYRULE":
		un_count[words[2],words[3]]=int(words[0]);
	if words[1] == "BINARYRULE":
		bi_count[words[2],words[3],words[4]]=int(words[0]);
f.close();
f = open(sys.argv[2],'w');
for key in un_count.keys():
		freq = float(un_count[key])/float(no_count[key[0]]);
		lines = key[0] + ' ' + key[1] + ' ' + str(freq)+'\n';
		f.write(lines);
f.close();
f = open(sys.argv[3],"w");
for key in bi_count.keys():
	freq = float(bi_count[key])/float(no_count[key[0]]);
	lines = key[0] + ' ' + key[1] + ' ' + key[2] + ' ' + str(freq)+'\n';
	f.write(lines);
f.close()
		
		